package controller;

import bean.Student;
import bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/query")
    public ModelAndView list(){
        ModelAndView modelAndView=new ModelAndView();
        List<Student> studentList=studentService.list();
        modelAndView.addObject("studentList",studentList);
        modelAndView.setViewName("/list.jsp");
        return modelAndView;
     }
    @RequestMapping("/queryreal")
    public String queryreal(){
        return "redirect:/student/query";
    }
    @RequestMapping("/add")
    public String add(Student student){
     studentService.add(student);
     return "redirect:/student/query";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable(value = "id",required = false)int id){
        studentService.delete(id);
        return "redirect:/student/query";
    }


    @RequestMapping("/update/{id}")
//    public String update(@PathVariable(value = "id",required = false)int id){
//        studentService.queryOne(id);
//        return "redirect:/student/update";
//    }
    public ModelAndView queryOne(@PathVariable(value = "id",required = false)int id){
        ModelAndView modelAndView=new ModelAndView();
        Student student=studentService.queryOne(id);
        modelAndView.addObject("student",student);
        modelAndView.setViewName("/update.jsp");
        return modelAndView;
    }
}
